
alias scrl='screen -ls'
alias scrc='screen -S '
alias scrd='screen -d'
alias scrr='screen -r '

#-------------------------------------------------------------------------------
# defind stack root path
MY_STACK_ROOT=$CMDER_ROOT/config/profile.d

if [ ! -e "$MY_STACK_ROOT/.command_collecion" ];then
    touch  "$MY_STACK_ROOT/.command_collecion"
    echo "create command stack file : $MY_STACK_ROOT/.command_collecion"
fi

if [ ! -e "$MY_STACK_ROOT/.directory_history" ];then
    touch  "$MY_STACK_ROOT/.directory_history"
    echo "create directory history file : $MY_STACK_ROOT/.directory_history"
fi


#-------------------------------------------------------------------------------
# command_stack
function my_command_stack(){
    command=`echo "$*"`
    # echo "${command}"

   if [ -z ${command} ];then
      echo -e "\033[31mnull command"
      exit 0
   fi

   cat $MY_STACK_ROOT/.command_collecion | while read LINE
   do
      if [ "${LINE}"  = "${command}" ]; then
         count=$[$count+1]
         return $count
      fi
   done

   count=`echo "$?"`
   if [ ${count} -eq 0 ];then
      echo -e "${command}" >> $MY_STACK_ROOT/.command_collecion
   fi
}

function print_command_stack(){
   count=0

   cat $MY_STACK_ROOT/.command_collecion | while read LINE
   do
      echo -e "\033[31;1m$count\033[0m" $LINE
      count=$[$count+1]
   done
   echo " "
}

function run_command(){
   if [ $# -le 0 ]; then
      echo "bash: need one arguments"
      exit 0
      return
   fi

   if [ $# -eq 1 ]; then
      TAG=$1
   fi

   if [ $# -ge 2 ]; then
      echo "bash: d: too many arguments"
      exit 0
      return
    fi

    #echo change_directory tag ${TAG}
    count=0
   cat $MY_STACK_ROOT/.command_collecion | while read LINE
   do
      if [ "${count}"  = "${TAG}" ];then
         echo ${LINE}
         eval ${LINE}
         return 0
      fi
      count=$[$count+1]
   done
}


#-------------------------------------------------------------------------------
# directory_stack
function my_directory_stack(){
   if [ ${OLDPWD} != ${PWD} ];then
      count=0
      history_dir=`tail -19 $MY_STACK_ROOT/.directory_history`

      #echo "${history_dir}"
      for LINE in `echo "${history_dir}" | cat`
      do
         #echo $LINE
         if [ ${LINE} != ${PWD} ]; then
            if [ ${count} -eq 0 ]; then
               echo $LINE > $MY_STACK_ROOT/.directory_history
            else
               echo $LINE >> $MY_STACK_ROOT/.directory_history
            fi
            count=$[$count+1]
         fi
      done

      if [ ${count} -eq 0 ];then
        echo ${PWD} > $MY_STACK_ROOT/.directory_history
      fi
      if [ ${count} -ne 0 ];then
        echo ${PWD} >> $MY_STACK_ROOT/.directory_history
      fi
   fi
}

function print_directory_stack(){
   count=0
   print_dir=`tac $MY_STACK_ROOT/.directory_history`

   for LINE in `echo "${print_dir}" | cat`
   do
      echo -e "\033[31;1m$count\033[0m" $LINE
      count=$[$count+1]
   done
   echo " "
}

function change_directory(){
   if [ $# -le 0 ]; then
      echo "bash: need one arguments"
      exit 0
      return
   fi

   if [ $# -eq 1 ]; then
      TAG=$1
   fi

   if [ $# -ge 2 ]; then
      echo "bash: d: too many arguments"
      exit 0
      return
    fi

    #echo change_directory tag ${TAG}
    count=0
    print_dir=`tac $MY_STACK_ROOT/.directory_history`

    for LINE in `echo "${print_dir}" | cat`
    do
       if [ ${count} == ${TAG} ];then
          #echo ${LINE}
          cd ${LINE}
          return
       fi
       count=$[$count+1]
    done
}


#-------------------------------------------------------------------------------
alias cc=my_command_stack
alias cs=print_command_stack
alias ce="vim $MY_STACK_ROOT/.command_collecion"
alias c=run_command

# only inject to windows git bash,wsl use zsh enhance dir stack
if [[ "$OSTYPE" =~ ^msys ]]; then
  export PROMPT_COMMAND=my_directory_stack
  alias d=change_directory
fi
alias ds=print_directory_stack
alias de="vim $MY_STACK_ROOT/.directory_history"

#set android development environment
export ANDROID_SDK_HOME=/home/cheney/tools/android-sdk-linux
export PATH=$PATH:${ANDROID_SDK_HOME}/tools:${ANDROID_SDK_HOME}/build-tools/28.0.3:/home/cheney/tools/android-sdk-linux/platform-tools/systrace

export PATH=/mnt/c/programMe/cmder/bin:$PATH
alias /c="/mnt/c"
alias /d="/mnt/d"
alias /e="/mnt/e"
alias /f="/mnt/f"

alias mlog='adb logcat | grep -i -e '
alias mlogc='adb logcat -c'
alias mloge='adb logcat | grep -e "AndroidRuntime: " -e "DEBUG   :" -e System.err:'
alias mgreperror='grep -r -e "AndroidRuntime: " -e "DEBUG   :" -e System.err: ./'
alias mgrepcameraerror='grep -r -e "AndroidRuntime: " -e "DEBUG   :" -e System.err: ./ | tee errorkey.txt | grep -A 20 com.sec.android.app.camera'

alias mremount='adb wait-for-device;adb root;adb remount;adb shell svc power stayon usb;adb logcat -G 25m'
alias mps='adb shell ps -A | grep -i -e '
alias mkill='adb shell kill -9 '
alias mdumpcam='adb shell dumpsys media.camera '

alias e.='explorer .'
alias subl="/mnt/c/Program\ Files/Sublime\ Text\ 3/sublime_text.exe"
alias npp="/mnt/c/Program\ Files\ \(x86\)/Notepad++/notepad++.exe"

alias cdcmder='cd /mnt/e/programMe/cmder'
alias cdwork='cd /mnt/e/work'
alias cddaily='today=`date "+%m%d"`;dailyDir="/mnt/e/work/daily/temp"${today};cd ${dailyDir}'
alias happyWork='today=`date "+%m%d"`;dailyDir="/mnt/e/work/daily/temp"${today};mkdir ${dailyDir};cd ${dailyDir};echo "\n have a nice day \n"'
